import axios from 'axios'
export default {
  init() {

  },
  // 打印日志
   mylog(val) {
    let element = document.getElementById('this.mylog')
    let old_val = element.innerHTML
    let date = new Date().toString().slice(16, 24)
    element.innerHTML = date + '&nbsp;&nbsp;' + val + '<br>' + old_val;
  },

  // 添加缩略图
  add_image(img_base64) {
    let img = document.createElement('img');
    img.src = "data:image/jpg;base64," + img_base64;
    img.style.width = '80px';
    img.style.height = '80px';
    document.getElementById('suoluetu').appendChild(img)
  },
  // 切换主摄像头视频模式，重新加载主摄像头分辨率
  view1_mode_change (val) {
    let data = {"camidx": '0', "mode": val}
    axios.post("http://localhost:8020/gpy/device=getresolution", JSON.stringify(data)).then((res)=>{
      let resolution_list = res.data.data.split("|");
      this.view1_resolution_selected = resolution_list[0]
      this.view1_resolution_list = []
      for(var i=0; i<resolution_list.length; i++){
        this.view1_resolution_list.push(resolution_list[i])
      }
    })
  },

  // 切换副摄像头视频模式，重新加载副摄像头分辨率
  view2_mode_change (val) {
    let data = {"camidx": '1', "mode": val}
    axios.post("http://localhost:8020/gpy/device=getresolution", JSON.stringify(data)).then((res)=>{
      let resolution_list = res.data.data.split("|");
      this.view2_resolution_selected = resolution_list[0]
      this.view2_resolution_list = []
      for(var i=0; i<resolution_list.length; i++){
        this.view2_resolution_list.push(resolution_list[i])
      }
    })
  },

  // 打开主摄像头
  open_view1 () {
    document.getElementById('view1').src = 'http://localhost:8020/gpy/video=stream&camidx=0?1'
    this.mylog('打开主摄像头成功')
  },

  // 关闭主摄像头
  close_view1 () {
    let data = {"camidx": "0"}
    axios.post("http://localhost:8020/gpy/video=close", JSON.stringify(data)).then(()=>{
      document.getElementById('view1').src = ''
      this.mylog('关闭主摄像头成功')
    })
  },

  // 打开副摄像头
  open_view2 () {
    document.getElementById('view2').src = 'http://localhost:8020/gpy/video=stream&camidx=1?1'
    this.mylog('打开副摄像头成功')
  },

  // 关闭副摄像头
  close_view2() {
    let data = {"camidx": "1"}
    axios.post("http://localhost:8020/gpy/video=close", JSON.stringify(data)).then(()=>{
      document.getElementById('view2').src = ''
      this.mylog('关闭副摄像头成功')
    })
  },

  // 旋转
  rotate (angle) {
    let data = {"camidx": '0', "rotate": String(angle)}
    axios.post("http://localhost:8020/gpy/video=rotate", JSON.stringify(data)).then((res)=>{
      this.mylog("旋转" + String(angle) + "度成功")
    })
  },

  // base64 转文件
  base64ToFile(base64Data, filename = "单据1") {
    var arr = base64Data.split(','),
      bstr = window.atob(arr[0]),
      n = bstr.length,
      u8arr = new Uint8Array(n)
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n)
    }
    let file = new File([u8arr], filename, {
      type: 'image/jpeg',
    })
    const tempData = {
      name: filename,
      raw: file,
      size: file.size,
      status: "ready",
    }

    return file
  },

  // 主头拍照
  view1_scan () {
    let data = {
      "filepath": "base64",
      "rotate": "0",
      "cutpage": "0",
      "camidx": "0",
      "ColorMode": "0",
      "quality": "3"
    }
    axios.post("http://localhost:8020/gpy/video=grabimage", JSON.stringify(data)).then((res)=>{
      this.add_image(res.data.photoBase64)
      this.base64ToFile(res.data.photoBase64)
      this.mylog("主头拍照成功")
      this.mylog('图片base64： ' + res.data.photoBase64)
    })
  },

  // 副头拍照
  view2_scan () {
    let data = {
      "filepath": "base64",
      "rotate": "0",
      "cutpage": "0",
      "camidx": "1",
      "ColorMode": "0",
      "quality": "3"
    }
    axios.post("http://localhost:8020/gpy/video=grabimage", JSON.stringify(data)).then((res)=>{
      this.add_image(res.data.photoBase64)
      this.mylog("副头拍照成功")
      this.mylog('图片base64： ' + res.data.photoBase64)
    })
  },

  // 展平拍照
  flat_scan () {
    let data = {
      "filepath": "",
      "rotate": "0",
      "camidx": "0",
      "cutpage": "0",
      "autoflat": {
        "flat": "1",
        "leftfilepath": "D://left.jpg",
        "rightfilepath": "D://right.jpg",
        "removefinger": "1",
        "doublepage": "1"
      }
    }
    axios.post("http://localhost:8020/gpy/video=autoflat", JSON.stringify(data)).then((res)=>{
      this.add_image(res.data.photoBase64)
      this.add_image(res.data.leftphotoBase64)
      this.add_image(res.data.rightphotoBase64)
      this.mylog("展平拍照成功")
    })
  },

  // 开启自动拍照
  open_auto_scan () {
    let data = {
      "movedetecflag": "1",
      "listpath": "D://a",
      "filepath": "hy"
    }
    axios.post("http://localhost:8020/gpy/video=movedetec", JSON.stringify(data)).then(()=>{
      this.add_image(res.data.data)
      this.mylog("这是自动拍摄的图片")
    })
  },

  // 关闭自动拍照
  close_auto_scan() {
    let data = {"movedetecflag": "0"}
    axios.post("http://localhost:8020/gpy/video=movedetec", JSON.stringify(data)).then(()=>{
      this.mylog('关闭自动拍照成功')
    })
  },

  // 条码识别
  getcode () {
    let data = {"time": "20"}
    axios.post("http://localhost:8020/gpy/barcode=get", JSON.stringify(data)).then((res)=>{
      for(let i=0; i<res.data.data.length; i++){
        this.mylog(res.data.data[i].barcodedata)
      }
      this.mylog("识别成功，条码数量" + res.data.data.length + "个，分别是：")
    })
  },

  // 获取指纹
  getbiokey () {
    let data = {"time": "20"}
    axios.post("http://localhost:8020/gpy/biokey=get", JSON.stringify(data)).then((res)=>{
      this.add_image(res.data.data)
      this.mylog("获取指纹成功")
    })
  },

  // 读取身份证
  getidcard () {
    axios.post("http://localhost:8020/gpy/card=idcard").then((res)=>{
      this.add_image(res.data.IDCardInfo.photoBase64)
      this.add_image(res.data.IDCardInfo.photoBase64_Z)
      this.add_image(res.data.IDCardInfo.photoBase64_F)
      this.mylog('身份证UID：' + res.data.IDCardInfo.strIDUID)
      this.mylog('身份证附加信息：' + res.data.IDCardInfo.appendMsg)
      this.mylog('身份证民族代码：' + res.data.IDCardInfo.nationCode)
      this.mylog('身份证性别代码：' + res.data.IDCardInfo.sexCode)
      this.mylog('身份证有效终止日期：' + res.data.IDCardInfo.validEnd)
      this.mylog('身份证有效起始日期：' + res.data.IDCardInfo.validStart)
      this.mylog('身份证发卡机构：' + res.data.IDCardInfo.issueOrgan)
      this.mylog('身份证号码：' + res.data.IDCardInfo.cardID)
      this.mylog('身份证地址：' + res.data.IDCardInfo.address)
      this.mylog('身份证生日：' + res.data.IDCardInfo.birthday)
      this.mylog('身份证性别：' + res.data.IDCardInfo.sex)
      this.mylog('身份证姓名：' + res.data.IDCardInfo.name)
    })
  },

  // 弹出签字窗口: GW500A & GW1000A
  sign_a() {
    let data = {
      "pos": {
        "top": "250",
        "left": "280",
        "width": "600",
        "height": "250"
      },
      "remark": "开始签名"
    }
    axios.post("http://localhost:8020/gpy/serialport=sign", JSON.stringify(data)).then((res)=>{
      this.mylog(res.data.message)
    })
  },

  // 获取签字图片：GW500A & GW1000A
  sign_a_get () {
    axios.post("http://localhost:8020/gpy/pendisplay=getsigndata").then((res)=>{
      this.add_image(res.data.data)
      this.mylog("获取签字图片成功")
    })
  },

  //---------------------------------------------视频录制-------------------------------------
  // 开始录制视频
  start_video() {
    let data = {
      "action": "start",
      "parameter": {
        "camidx": "0",
        "width": "640",
        "height": "480",
        "audio": "",
        "framerate": "10",
        "filepath": "",
        "bit_rate": "400000"
      }
    }
    axios.post("http://localhost:8020/gpy/video=record", JSON.stringify(data)).then((res)=>{
      if(res.data.code == '0'){
        this.mylog("视频录制中..., 文件存储地址：" + res.data.filepath)
      }else{
        this.mylog("开始录制失败")
      }
    })
  },

  // 结束录制视频
  end_video() {
    let data = {"action": "stop"}
    axios.post("http://localhost:8020/gpy/video=record", JSON.stringify(data)).then((res)=>{
      if(res.data.code == '0'){
        this.mylog('录制时长：' + res.data.time)
      }else{
        this.mylog("结束录制视频失败")
      }
    })
  },

  // 获取录制视频状态
  get_state_video () {
    let data = {"action": "status"}
    axios.post("http://localhost:8020/gpy/video=record", JSON.stringify(data)).then((res)=>{
      this.mylog("提示：设备状态，100:空闲，101:录像中，102:设备错误")
      this.mylog("当前状态：" + res.data.status)
    })
  },

  // 获取音频列表
  get_audio () {
    let data = {"action": "audio"}
    axios.post("http://localhost:8020/gpy/video=record", JSON.stringify(data)).then((res)=>{
      this.mylog("音频设备列表：" + res.data.audio)
    })
  },

  //---------------------------------------------A3A4幅面自动切换-----------------------------
  // 开启A3A4幅面自动切换
  open_a3a4 () {
    let data = {
      "switchflag": "1",
      "a3size": "0.5",
      "a4size": "0.9"
    }
    axios.post("http://localhost:8020/gpy/device=a3a4switch", JSON.stringify(data)).then((res)=>{
      if(res.data.code == '0'){
        this.mylog("开启幅面自动切换成功")
      }else{
        this.mylog("开启幅面自动切换失败")
      }
    })
  },

  // 关闭A3A4幅面自动切换
  close_a3a4 () {
    let data = {
      "switchflag": "0",
      "a3size": "0.5",
      "a4size": "0.9"
    }
    axios.post("http://localhost:8020/gpy/device=a3a4switch", JSON.stringify(data)).then((res)=>{
      if(res.data.code == '0'){
        this.mylog("关闭幅面自动切换成功")
      }else{
        this.mylog("关闭幅面自动切换失败")
      }
    })
  },

  //---------------------------------------------pdf-----------------------------------------
  // 拍照并加入PDF队列
  add_pdf() {
    let data1 = {
      "filepath": "base64",
      "rotate": "0",
      "cutpage": "0",
      "camidx": "0",
      "quality": "5"
    }
    axios.post("http://localhost:8020/gpy/video=grabimage", JSON.stringify(data1)).then((res1)=>{
      if(res1.data.code == '0'){
        this.add_image(res1.data.photoBase64)
        
        let data2 = {
          "ImagePath": "",
          "ImageBase64": res1.data.photoBase64
        }
        axios.post("http://localhost:8020/gpy/pdf=addimage", JSON.stringify(data2)).then((res2)=>{
          if(res2.data.code == '0'){
            this.mylog("拍照成功，并加入到PDF队列中")
          }else{
            this.mylog("加入PDF队列失败")
          }
        })
      }else{
        this.mylog("拍照失败，请重新拍摄")
      }
    })
  },

  // 生成PDF文件
  save_pdf(){
    let data = {"PdfPath": "D://pdf.pdf"}
    axios.post("http://localhost:8020/gpy/pdf=save", JSON.stringify(data)).then((res)=>{
      if(res.data.code == '0'){
        this.mylog("PDF保存成功")
      }
    })
  },

  //---------------------------------------------人脸-----------------------------------------
  // 活体检测
  is_living_person() {
    let data = {"time": "20"}
    axios.post("http://localhost:8020/gpy/faceLive=start", JSON.stringify(data)).then((res)=>{
      this.mylog("提示：比对结果，-1:失败；0:图片；1:成功")
      this.mylog("检测结果：" + res.data.data)
    })
  },

  // 人证比对
  person_and_IDCard() {
    // 1.读取身份证
    axios.post("http://localhost:8020/gpy/card=idcard").then((res1)=>{
      if(res1.data.code == '0'){
        this.add_image(res1.data.IDCardInfo.photoBase64)
        // 2.拍摄人脸
        let data2 = {
          "filepath": "base64",
          "rotate": "0",
          "cutpage": "0",
          "camidx": "1",
          "quality": "5"
        }
        axios.post("http://localhost:8020/gpy/video=grabimage", JSON.stringify(data2)).then((res2)=>{
          this.add_image(res2.data.photoBase64)
          // 3.比对
          let data3 = {
            "FaceOne": res1.data.IDCardInfo.photoBase64,
            "FaceTwo": res2.data.photoBase64
          }
          axios.post("http://localhost:8020/gpy/comparison=imgdata", JSON.stringify(data3)).then((res3)=>{
            this.mylog("提示：比对值大于50可以认为是同一个人")
            this.mylog("比对值：" + res3.data.data)
          })
        })
      }else{
        this.mylog("请放身份证，重新点击此按钮")
      }
    })
  },

  // ocr
  ocr(){
    // ocr 是从图片中识别的，所以需要先拍一张图片保存本地，然后在做ocr识别
    let data1 = {
      "filepath": "",
      "rotate": "0",
      "cutpage": "0",
      "camidx": "0"
    } 
    axios.post("http://localhost:8020/gpy/video=grabimage", JSON.stringify(data1)).then((res1)=>{
      this.add_image(res1.data.photoBase64)
      this.mylog("识别中。。。")
      let data2 = {
        "ocrflag": "1",
        "picfilepath": res1.data.filepath,
        "savefilepath": "D://ocr.pdf"
      }
      axios.post("http://localhost:8020/gpy/video=ocr", JSON.stringify(data2)).then((res2)=>{
        if(res2.data.code == '0'){
          this.mylog("识别成功")
        }else{
          this.mylog("识别失败")
        }
      })
    })
  },

  //---------------------------------------------设备-----------------------------------------
  // 获取设备型号
  getequipmenttype() {
    axios.post("http://localhost:8020/gpy/device=getequipmenttype").then((res)=>{
      this.mylog("设备型号：" + res.data.data)
    })
  },

  // 获取设备序列号
  getsonixserialnumber() {
    axios.post("http://localhost:8020/gpy/device=getsonixserialnumber").then((res)=>{
      this.mylog("设备序列号：" + res.data.data)
    })
  },

  // 获取设备状态
  get_state() {
    axios.post("http://localhost:8020/gpy/video=status").then((res)=>{
      this.mylog("提示：no:未连接；ok:已连接；run:已连接且运行")
      this.mylog("副摄像头：" + res.data.video1)
      this.mylog("主摄像头：" + res.data.video0)
    })
  },

  // 判断设备是否连接
  is_con() {
    axios.post("http://localhost:8020/gpy/device=isconnect").then((res)=>{
      this.mylog("设备连接数：" + res.data.data)
    })
  },

  //---------------------------------------------串口-----------------------------------------
  // 打开串口
  open_serialport() {
    let data = {
      "port": "0",
      "baud": "115200",
      "parity": "0",
      "databits": "8",
      "stopbits": "0"
    }
    axios.post("http://localhost:8020/gpy/serialport=initserialport", JSON.stringify(data)).then((res)=>{
      if(res.data.code == '0'){
        this.mylog("打开串口成功")
      }
    })
  },

  // 关闭串口
  close_serialport(){
    let data = {"port": "0"}
    axios.post("http://localhost:8020/gpy/serialport=deinitserialport", JSON.stringify(data)).then((res)=>{
      if(res.data.code == '0'){
        this.mylog("关闭串口成功")
      }
    })
  },

  // 弹出签字窗口: GW500R & GW1000R
  sign_r(){
    axios.post("http://localhost:8020/gpy/serialport=sign").then((res)=>{
      if(res.data.code == '0'){
        this.mylog('弹出签字窗口成功')
      }
    })
  },

  // 获取签名图片
  sign_r_get(){
    axios.post("http://localhost:8020/gpy/serialport=getdata").then((res)=>{
      this.add_image(res.data.data)
      this.mylog("获取签名图片成功")
    })
  },
}